//
// Add new jQuery object method to set-up the form validation
//

jQuery.fn.speedyValidate = function (settings) {
	
	return this.each(function() {
							  
				
		// Find any input fields that have validation configured
		
		$(this).find('[class*=validate][type!=checkbox]').focusout(function() {
			
			$.SpeedyValidationEngine.validate(this);
			
		});
				
		
				
				
	});
	
};


//
// Our validation engine 
//

jQuery.SpeedyValidationEngine = {
	
	// Function to check fields validation
	validate : function(caller) {
		

		// Get value of the field
		var field_val = $.trim($(caller).val());
		
		$.SpeedyValidationEngine.show_error_text = true;
		
		var rules = $.SpeedyValidationEngine.load_validation(caller);
		
		var no_errors = true;
		
		$.each(rules,function(index,value){
			
			if (no_errors) {
			
				switch(value) {
				
					case 'notext':
						$.SpeedyValidationEngine.show_error_text = false;
						break;
				
					case 'required':
						no_errors = $.SpeedyValidationEngine.check_required(caller);
						break;
					
					case 'email':
						no_errors = $.SpeedyValidationEngine.check_email(caller);
						break;
						
					case 'uk_postcode':
						no_errors = $.SpeedyValidationEngine.check_uk_postcode(caller);
						break;
						
					case 'confirm':
						no_errors = $.SpeedyValidationEngine.check_confirm(caller,rules,index);
						break;
						
					case 'minlength':
						no_errors = $.SpeedyValidationEngine.check_minlength(caller,rules,index);
						break;
					
						
				}
			
			}
			
	
		});

		


		
	},
	load_validation : function(caller) {
	
		// Get everything between the brackets
		rulesParsing = $(caller).attr('class');
		rulesRegExp = /\[(.*)\]/;
		getRules = rulesRegExp.exec(rulesParsing);

        if (getRules == null) return false;
		
		// Now get the individual rules
		str = getRules[1];
		pattern = /\[|,|\]/;
		result = str.split(pattern);
		

		return result;


	},
	show_success : function(caller,msg) {
		$(caller).next('.speedyvalidate').remove();
		if (!$.SpeedyValidationEngine.show_error_text)
			msg = '';
		$(caller).after('<span class="speedyvalidate success">'+msg+'</span>');
	},
	show_failure : function(caller,msg) {
		$(caller).next('.speedyvalidate').remove();
		if (!$.SpeedyValidationEngine.show_error_text)
			msg = '';
		$(caller).after('<span class="speedyvalidate failure">'+msg+'</span>');
	},
	check_required : function(caller) {
		
		if ($(caller).val() != "") {
			$.SpeedyValidationEngine.show_success(caller,'');
			return true;
		} else {
			$.SpeedyValidationEngine.show_failure(caller,'This field is required');
			return false;
		}
		
	},
	check_email : function(caller) {
		
		var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		if ( regex.test($(caller).val()) ) { 
		
			$.SpeedyValidationEngine.show_success(caller,'');
			return true;
			
		} else {
			
			$.SpeedyValidationEngine.show_failure(caller,'Not a valid email address');
			return false;
			
		}

	},
	check_uk_postcode : function(caller) {
		
		var field_value = $(caller).val();
		
		var regex = /^[A-Z]{1,2}[0-9R][0-9A-Z]? ?[0-9][ABD-HJLNP-UW-Z]{2}$/i;
		
		if ( regex.test($(caller).val()) ) { 
		
		
			// And lets try format the postcode nicely also

			var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2}[A-Z]?)([0-9][A-Z]{2}$)/i;
			field_value = field_value.toUpperCase();
			field_value = field_value.replace(postcodeRegEx,"$1 $2");

			$(caller).val(field_value);
			
			
			$.SpeedyValidationEngine.show_success(caller,'');
			
			return true;
			
			
			
		} else {
			
			$.SpeedyValidationEngine.show_failure(caller,'Not a valid UK Postcode');
			return false;
			
		}
		
	},
	check_confirm : function(caller,rules,index) {
		
		var field_value = $(caller).val();
		
		var confirm_value = $('#'+rules[index+1]).val();
		
		if (field_value != confirm_value) {
			
			$.SpeedyValidationEngine.show_failure(caller,'This does not match the other');
			return false;
			
		} else {
			
			$.SpeedyValidationEngine.show_success(caller,'');
			return true;
		}
		
		
	},
	check_minlength : function(caller,rules,index) {
		
		var field_value = $(caller).val();
		
		var min_length = rules[index+1];
		
		if (field_value.length < min_length) {
			
			$.SpeedyValidationEngine.show_failure(caller,'Must be at least ' + min_length + ' characters long');
			return false;
			
		} else {
			
			$.SpeedyValidationEngine.show_success(caller,'');
			return true;
		}
		
		
	}
	
};














